//
//  NetViewController.m
//  NetTest
//
//  Created by mac on 12-5-5.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NetViewController.h"
#import "JSONKit.h"
#import "HtmlParase.h"
#import "TicketResult.h"
#import "TicketResultService.h"
#import "HttpData.h"
#import "KL2.h"

@implementation NetViewController
@synthesize lbText=_lbText;
@synthesize tableview;
@synthesize txtCount;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    
    if (self) {
        
        serv = [[TicketResultService alloc] init];
        // Custom initialization

        NSLog(@"Setp %@",NSStringFromSelector(_cmd));
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    //[[Reachability sharedReachability] setNetworkActivityIndicatorVisible:YES];
    
    Reachability *rea = [Reachability reachabilityWithHostName:@"www.abaidu.com"]; 
    switch ([rea currentReachabilityStatus]) {
        case NotReachable:
            _lbText.text=@"NotReachable";
            
            break;
        case ReachableViaWiFi:
            _lbText.text=@"Wifi";
            break;
        case ReachableViaWWAN:
            _lbText.text=@"3G";
            break;
        default:
            break;
    }
        // arr = [[serv getCollection:nil orderby:@"sectionID desc" startIndex:0 count:50] retain];
   // [self getcount];
    //_lbText.text=NSStringFromSelector(_cmd);
    // Do any additional setup after loading the view from its nib.
}

-(void)getcount{
    //TicketResultService *serv=[[[TicketResultService alloc] init] autorelease];
    
    int count=[serv getCountBystrwhere:nil];
    _lbText.text=[NSString stringWithFormat:@"Count =%i ",count];
}


-(IBAction)getLastClick:(id)sender{
    TicketResult* last = [serv getLast];
    
    if (last!=nil) {
        _lbText.text=[NSString stringWithFormat:@"期号:%@ 号码:%@",last.sectionID,last.result];
    }
}

-(IBAction)deleteAll:(id)sender{
    
    //[[HttpData HttpDataInstance] sentpostData];
//    TicketResultService *serv=[[[TicketResultService alloc] init] autorelease];
    
    [serv deleteAll];
    [self getcount];
}

-(IBAction)getLast50Click:(id)sender{
//    [tableview beginUpdates];
    if (arr!=NULL) {
        [arr release];
    }
    
    
    int count=50;//
    if (![txtCount.text isEqualToString:@""]) {
        count = [txtCount.text intValue];
        if (count<50) {
            count=50;
        }
    }

    
    arr = [[serv getCollection:nil orderby:@"sectionID desc" startIndex:0 count:count] retain];
    //tableview.dataSource=nil;
    //tableview.dataSource=self;
    [tableview reloadData];
    
    KL2 *kl=[[[KL2 alloc] init] autorelease];
    
    [kl createNumGroup];
//    int i=0;
//    for (TicketResult* r in arr) {
//       //UITableViewCell *c = [tableview cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:i++]];
//        
//         UITableViewCell *c=[[[UITableViewCell alloc] init] autorelease];
//        [c.textLabel setText:[NSString stringWithFormat:@"%@:%@",r.sectionID,r.result]];
//        //[tableview addSubview:c];
//        
//    }
//    [tableview insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
//    [tableview endUpdates];
}
-(IBAction)getPost:(id)sender{
    TicketResult *lastresult=[serv getLast];
    
    if (lastresult!=nil) {
        
        FCLOG(@"最新一期为:%@ 号码为:%@",lastresult.sectionID,lastresult.result);
        
        [HtmlParase getPostResult:lastresult.sectionID];
    }
    [self getcount];
}

-(IBAction)btnJsonClick:(id)sender
{
    _lbText.text=NSStringFromSelector(_cmd);
    
//    
//    TicketResultService *tickservice=[[[TicketResultService alloc] init] autorelease];
//    
//    TicketResult *result=[[TicketResult alloc] init];
//    
//    result.sectionID=@"20120501083";
//    //result.type=[[[NSNumber alloc] initWithInt:1] autorelease];
//    result.result=@"08 09 10 20 11 03 07 01";
//    
//    //[tickservice addModel:result];
//    
//   //TicketResult* r=(TicketResult*)[tickservice getModel:@"20120509084"];
//    
//    TicketResult *r=nil;
//    
//    r = (TicketResult *)[tickservice getModel:@"20120509084" ];
//    
//    NSLog(@"getmodel id=%@ ",r.result);
    //JSONDecoder *decoder=[[JSONDecoder alloc] init];
//
//    NSString *str=@"{\"Name\":\"丘国伟\"}";
//    
//    NSDictionary* dic = [str objectFromJSONString];
//    
//    NSString *name = [dic objectForKey:@"Name"];
//    
//    NSLog(@"%@",name);
//    
//    _lbText.text=name;
//    
//   NSString *str2 = [name JSONString];
//    _lbText.text=str2;
    [HtmlParase getTickResult];
    _lbText.text=@"End";
    [self getcount];
}

#pragma mark TableView

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier=@"Contact";
    
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (cell==nil) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier] autorelease];
    }
    
   // 
    TicketResult* result = (TicketResult *)[arr objectAtIndex:indexPath.row];

    NSString *cellvalue=[NSString stringWithFormat:@"%03i 期号:%@\t号码:%@",indexPath.row+1, result.sectionID,result.result];;
    cell.textLabel.text=cellvalue;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arr count];
}

-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (arr!=NULL&&[arr count]>0) {
        return [NSString stringWithFormat:@"显示最新的%i期数据",[arr count]];
    }
    else{
        return @"";
    }
}

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"CopyRight:FCPLAY";
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

-(void)dealloc{
    [arr release];
    [serv release];
    
    [_lbText dealloc];
    [tableview dealloc];
    [txtCount dealloc];
    
    [super dealloc];
}

@end
